package cn.com.blackboard.DataModule4B2.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cm.com.blackboard.DataModule4B2.customizeConfigUtils.BatchImportUtil;
import cn.com.blackboard.DataModule4B2.BBDataUtils.CourseMembershipUtil;
import cn.com.blackboard.DataModule4B2.dao.StudentCourseInfoMapper;
import cn.com.blackboard.DataModule4B2.model.StudentCourseInfo;
import cn.com.blackboard.DataModule4B2.service.StudentCourseInfoService;
import cn.com.blackboard.DataModule4B2.util.BatchResultModel;
import cn.com.blackboard.DataModule4B2.util.BatchResultModel.Status;
import cn.com.blackboard.DataModule4B2.util.Condition;

@Service("StudentCourseInfoService")
public class StudentCourseInfoServiceImpl implements StudentCourseInfoService{

	private static final Logger logger = LoggerFactory.getLogger(StudentCourseInfoServiceImpl.class);
	@Autowired
	private StudentCourseInfoMapper StudentCourseInfoMapper;
	
	public int insert(StudentCourseInfo StudentCourseInfo) {
		// TODO Auto-generated method stub
		return StudentCourseInfoMapper.insert(StudentCourseInfo);
	}

	@Override
	public List<StudentCourseInfo> getStudentCourseInfo() {
		return StudentCourseInfoMapper.selectByCondition(Condition.SearchAll);
	}

	@Override
	public int updateStatus(String StudentCourseInfoId) {
		// TODO Auto-generated method stub
		return StudentCourseInfoMapper.updateRecordImportedByPK1(StudentCourseInfoId);
	}
	
	@Override
	public List<StudentCourseInfo> getStudentCourseInfo(String jsonCondition) {
		Condition condition = Condition.ParseCondition(jsonCondition);
		String conditionStr = Condition.RenderConditoin(condition);
		return StudentCourseInfoMapper.selectByCondition(conditionStr);
	}

	@Override
	public List<BatchResultModel> importStudentCourseInfo(List<StudentCourseInfo> StudentCourseInfoLst) {
		logger.info("start to import StudentCourseInfo");
		int count = 0;
		List<BatchResultModel> list = new ArrayList<BatchResultModel>();
		for(StudentCourseInfo sci:StudentCourseInfoLst){
			BatchResultModel batchResultModel = new BatchResultModel();
			batchResultModel.setModelClsName(StudentCourseInfo.class.getName());
			batchResultModel.setModelClsAttr("courseId-userId");
			batchResultModel.setValue(sci.getCourseId()+"-"+sci.getUserId());
			Status status = CourseMembershipUtil.importStudentCourseMembership(sci);
			batchResultModel.setStatus(status);
			count += status.isSuccess()?1:0;
			if(status.isSuccess())
				updateStatus(sci.getUserId());
			list.add(batchResultModel);
		}
		logger.info("studentEnrollment need to be imported is "+StudentCourseInfoLst.size()+" the num imported indeed is "+count);
    	return list;
	}

	@Override
	public List<BatchResultModel> batchImportStudentCourseInfo() {
		String conditionStr = BatchImportUtil.getBatchFilterCondition(StudentCourseInfo.class.getName());
		List<StudentCourseInfo> studentCourseInfoList = StudentCourseInfoMapper.selectByCondition(conditionStr);
		List<BatchResultModel> list = importStudentCourseInfo(studentCourseInfoList);
		return list;
	}
}
